package com.mysoft.gfyx.clue.controller;

import com.mysoft.framework.common.exception.BusinessLogicException;
import com.mysoft.framework.common.util.MappingUtil;
import com.mysoft.framework.modeling.dataapi.DataApi;
import com.mysoft.framework.modeling.dto.ListDataResult;
import com.mysoft.framework.modeling.dto.LoadDataParams;
import com.mysoft.framework.mvc.api.Controller;
import com.mysoft.framework.rpc.annotation.PubAction;
import com.mysoft.framework.rpc.annotation.PubService;
import com.mysoft.framework.rpc.contants.RequestPrefix;
import com.mysoft.gfyx.clue.controller.dto.*;
import com.mysoft.gfyx.clue.dao.dto.ClueFollowRecordsDto;
import com.mysoft.gfyx.clue.dao.entity.Clue;
import com.mysoft.gfyx.clue.service.ClueService;
import com.mysoft.gfyx.clue.service.CluecontacterService;
import com.mysoft.gfyx.clue.service.CluefollowrecordService;
import com.mysoft.gfyx.clue.service.dto.ClueContacterDto;
import com.mysoft.gfyx.clue.service.dto.ClueDto;
import com.mysoft.gfyx.clue.service.dto.ClueFollowRecordDto;
import com.mysoft.gfyx.common.rpc.opptyservice.OpptyServiceClientFactoryService;
import com.mysoft.gfyx.common.rpc.opptyservice.OpptyServiceClientService;
import com.mysoft.gfyx.common.rpc.opptyservice.dto.*;
import com.mysoft.gfyx.common.util.PermUtil;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMethod;

import java.util.List;
import java.util.UUID;

@PubService(value = "/clueDataApi", prefix = RequestPrefix.API)
@Tag(name = "线索管理-API")
public class ClueDataApiController extends Controller implements DataApi {

    @Autowired
    private CluefollowrecordService cluefollowrecordService;

    @Autowired
    private CluecontacterService cluecontacterService;

    @Autowired
    private ClueService clueService;

    @Autowired
    private OpptyServiceClientFactoryService opptyServiceClientFactoryService;

    @Autowired
    private PermUtil permUtil;

    @PubAction(value = "/addClueFollowRecord", method = RequestMethod.POST)
    @Transactional
    public boolean addClueFollowRecord(@RequestBody AddClueFollowRecordRequestDto dto) {
        // 添加跟进记录
        ClueFollowRecordDto clueFollowRecordDto = MappingUtil.getSimpleMapperFacade().map(dto, ClueFollowRecordDto.class);
        cluefollowrecordService.addClueFollowRecord(clueFollowRecordDto);

        // 更改线索状态
        clueService.changeCooperationStatus(dto.getClueStatus(), dto.getClueGUID());
        return true;
    }

    @PubAction(value = "/getClueFollowRecords", method = RequestMethod.POST)
    public GetClueFollowRecordsResponseDto getClueFollowRecords(@RequestBody GetClueFollowRecordsRequestDto dto) {
        UUID clueId = UUID.fromString(dto.getClueId());
        GetClueFollowRecordsResponseDto response = new GetClueFollowRecordsResponseDto();
        List<ClueFollowRecordsDto> list = cluefollowrecordService.getClueFollowRecordsByClueId(clueId);

        response.setList(list);
        return response;
    }

    @PubAction(value = "/getClueSourceFollowRecords", method = RequestMethod.POST)
    public GetClueFollowRecordsResponseDto getClueSourceFollowRecords(@RequestBody GetClueSourceFollowRecordsRequestDto dto) {
        ClueDto clueDto = clueService.getClueBySource(dto);
        GetClueFollowRecordsResponseDto response = new GetClueFollowRecordsResponseDto();
        List<ClueFollowRecordsDto> list = cluefollowrecordService.getClueFollowRecordsByClueId(clueDto.getClueGUID());

        response.setList(list);
        return response;
    }

    @PubAction(value = "/addClue", method = RequestMethod.POST)
    public boolean addClue(@RequestBody AddClueRequestDto request) {
        // check is permission
        boolean enableCluePerm = permUtil.checkBusinessUnit(permUtil.clueActionCode);
        if (!enableCluePerm) {
            throw new BusinessLogicException("当前操作没有权限，需要操作请购买【线索雷达】模块");
        }

        clueService.checkAndAddClue(request);
        return true;
    }

    @PubAction(value = "/followClue")
    public boolean followClue(@RequestBody AddClueRequestDto request) {
        if (request.getClueGUID() != null && !request.getClueGUID().toString().equals("")) {//update
            ClueDto clueDto = MappingUtil.getSimpleMapperFacade().map(request, ClueDto.class);
            return clueService.updateById(clueDto, request.getClueGUID());
        } else {//add操作
            Clue clue = clueService.checkAndAddClue(request);

            ClueFollowRecordDto clueFollowRecordDto = new ClueFollowRecordDto();
            clueFollowRecordDto.setClueGUID(clue.getClueGUID());
            ClueContacterDto clueContacterDto = cluecontacterService.getClueContacterByClueTel(clue.getClueGUID(), request.getContacterTel());
            clueFollowRecordDto.setContacterGUID(clueContacterDto.getClueContacterGUID());
            clueFollowRecordDto.setContactWay(request.getFollowType());
            clueFollowRecordDto.setFollowRecord(request.getFollowContent());
            clueFollowRecordDto.setContactWayStatus(request.getFollowStatus());
            clueFollowRecordDto.setFrom_device("pc");

            cluefollowrecordService.addClueFollowRecord(clueFollowRecordDto);
        }

        return true;
    }

    @PubAction(value = "/getBuAndUser", method = RequestMethod.POST)
    public Object getBuAndUser() {
        OpptyServiceClientService opptyService = opptyServiceClientFactoryService.getOpptyServiceClientService();
        GetBuAndUserResponseDto res = opptyService.getBuAndUser();
        if (res != null && res.getData() != null) {
            return res.getData();
        }
        return new GetBuAndUserResponseDto.Bu();
    }

    @PubAction(value = "/changeResponsiblePerson", method = RequestMethod.POST)
    public Object changeResponsiblePerson(@RequestBody ChangeResponsiblePersonRequestDto request) {
        OpptyServiceClientService opptyService = opptyServiceClientFactoryService.getOpptyServiceClientService();
        ChangeResponsiblePersonResponseDto res = opptyService.changeResponsiblePerson(request);
        if (res != null && res.getData() != null) {
            return res.getData();
        }
        return "";
    }

    @PubAction(value = "/getClueResponsiblePerson", method = RequestMethod.POST)
    public Object getClueResponsiblePerson(@RequestBody GetClueResponsiblePersonRequestDto request) {
        OpptyServiceClientService opptyService = opptyServiceClientFactoryService.getOpptyServiceClientService();
        GetClueResponsiblePersonResponseDto res = opptyService.getClueResponsiblePerson(request.getClue_guid());
        if (res != null && res.getData() != null) {
            return res.getData();
        }
        return new GetClueResponsiblePersonResponseDto.User();
    }
}
